using System;
using System.Collections.Generic;
using System.Text;
using BizTalkCop.Rules.Bases;
using Microsoft.FxCop.Sdk;
using Microsoft.BizTalk.ExplorerOM;

namespace BizTalkCop.Rules.Naming.Rules
{
    /// <summary>
    /// Rule verifying receive location names are correctly suffixed with their transport type
    /// </summary>
    public class ReceiveLocationNamesShouldIndicateTransport : ApplicationRuleBase
    {
        /// <summary>
        /// Default constructor
        /// </summary>
        public ReceiveLocationNamesShouldIndicateTransport() : base("ReceiveLocationNamesShouldIndicateTransport") { }

        /// <summary>
        /// Checks if the receive location name is correctly suffixed
        /// </summary>
        /// <param name="application">Application to check</param>
        /// <returns>Problem collection</returns>
        protected override ProblemCollection Check(Application application)
        {
            foreach (ReceivePort port in application.ReceivePorts)
            {
                foreach (ReceiveLocation location in port.ReceiveLocations)
                {
                    string suffix = string.Format("{0}{1}", this.GetConfiguredSetting(Setting.Separator), location.TransportType.Name);
                    if (!location.Name.EndsWith(suffix))
                    {
                        Problems.Add(new Problem(GetNamedResolution("Suffix", location.Name, suffix), application.Name));
                    }
                }
            }
            return Problems;
        }

        #region Configured settings

        static ReceiveLocationNamesShouldIndicateTransport()
        {
            Dictionary<string, string> defaults = new Dictionary<string, string>();
            defaults.Add(Setting.Separator, Default.Separator);
            SetDefaults(typeof(ReceiveLocationNamesShouldIndicateTransport), defaults);
        }

        private struct Setting
        {
            public const string Separator = "Separator";
        }

        private struct Default
        {
            public const string Separator = ".";
        }

        #endregion
    }
}
